 <!DOCTYPE html>
<html>
<head>
<title>test.html</title>

<meta name="keywords" content="keyword1,keyword2,keyword3">
<meta name="description" content="this is my page">
<meta name="content-type" content="text/html; charset=UTF-8">

<link rel="stylesheet" href="../resources/css/condition.css">
</head>

<body>
	<div class="background" style="">
		<div class="row">
			<span>门诊: </span> <input id="clinic" type="text" disabled>
		</div>
		<div class="row">
			<span>类别: </span> <select id="type" name="type">
				<option value="0">月报</option>
				<option value="1">季报</option>
			</select>
		</div>
		<div id = "div_month" class="row">
			<span>时间: </span> <select id="monthDate" name="monthDate">
			</select>
		</div>
		<div id = "div_season" class="row">
			<span>时间: </span> <select id="seasonDate" name="seasonDate">
			</select>
		</div>
	</div>
</body>
<script type="text/javascript" src="../lib/jquery.min.js"></script>
<script>
	//初始化参数
	$(function() {
		var userInfo = JSON.parse(localStorage.getItem("loginUser"));
		$("#clinic").val(userInfo.clinicName);
		$("#clinic").attr("_id", userInfo.clinicId);
		
		initMonthSelect();//初始化月报下拉框
		intSeasonSelect();//初始化季报下拉框
		
		$("#div_month").show();
		$("#div_season").hide();
	});
	
	//初始化报表参数
	function initParam() {
		if($("#type").val() == 0) {
			//月报
			var clinicFundReport = {
				clinicId : $("#clinic").attr("_id"),
				clinicName : $("#clinic").val(),
				startTime : $("#monthDate option:selected").attr("_starttime"),
				endTime : $("#monthDate option:selected").attr("_endtime"),
			};
			localStorage.setItem("clinicFundReport", JSON.stringify(clinicFundReport));
		}
		if($("#type").val() == 1) {
			//季报
			var clinicFundReport = {
				clinicId : $("#clinic").attr("_id"),
				clinicName : $("#clinic").val(),
				startTime : $("#seasonDate option:selected").attr("_starttime"),
				endTime : $("#seasonDate option:selected").attr("_endtime"),
			};
			localStorage.setItem("clinicFundReport", JSON.stringify(clinicFundReport));
		}
	}
	
	//初始化月报下拉框
	function initMonthSelect(){
		var nowDate = new Date();//当前时间
		var year = nowDate.getFullYear();
		var month = nowDate.getMonth() + 1;
		var content; //2017年XX期
		var startTime; //开始时间
		var endTime; //结束时间
		for (var i = 0; i < 12; i++) {
			if (month == 0) {
				year = year - 1;
				month = 12;
				content = year + "年" + month + "期";
				startTime = year + "-" + month + "-" + "01";
				endTime = (year+1) + "-1-01";
				month = month - 1;
			} else {
				content = year + "年" + month + "期";
				startTime = year + "-" + month + "-" + "01";
				if(month == 12) {
					endTime = (year+1) + "-1-01";
				}else {
					endTime = year + "-" + (month+1) + "-" + "01";
				}
				month = month - 1;
			}
			$("#monthDate").append("<option _startTime=" + startTime +" _endTime=" + endTime +">" + content + "</option>");
		}
	}
	
	//初始化季报下拉框
	function intSeasonSelect(){
		var nowDate = new Date();//当前时间
		var year = nowDate.getFullYear();
		var month = nowDate.getMonth() + 1;
		var content; //2017年XX期
		var startTime; //开始时间
		var endTime; //结束时间
		var number;//季度数
		if (month <= 3) {
			number = 1;
		} else if (month <= 6) {
			number = 2;
		} else if (month <= 9) {
			number = 3;
		} else {
			number = 4;
		}
		
		for (var i = 0; i < 5; i++) {
			if(number == 0) {
				year = year - 1;
				number = 4;
				content = year + "年第" + number + "季度";
				startTime = year + "-" + getSeasonMonth(number) + "-" + "01";
				endTime = (year+1) + "-1-01";
				number = number - 1;
			}else {
				content = year + "年第" + number + "季度";
				startTime = year + "-" + getSeasonMonth(number) + "-" + "01";
				if(number == 4){
					endTime = (year+1) + "-1-01";
				}else {
					endTime = year + "-" + (getSeasonMonth(number)+3) + "-" + "01";
				}
				number = number - 1;
			}
			$("#seasonDate").append("<option _startTime=" + startTime +" _endTime=" + endTime +">" + content + "</option>");
		}
	}
	
	//类别变换事件
	$(function() {
		$("#type").change(function() {
			if ($(this).val() == 0) {
				//月报
				$("#div_month").show();
				$("#div_season").hide();
			}
			if ($(this).val() == 1) {
				//季报
				$("#div_month").hide();
				$("#div_season").show();
			}
		});
	});
	
	//获取季报值对应的月份
	function getSeasonMonth(number) {
		if(number == 1) {
			return 1;
		}
		if(number == 2) {
			return 4;
		}
		if(number == 3) {
			return 7;
		}
		if(number == 4) {
			return 10;
		}
	}
</script>
</html>
